﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerChontroller : MonoBehaviour
{
    private Animator anim;
    private float horizontal;
    private float vertical;
    private Transform mainCamera;
    //旋转的速度
    private float changDirSpeed = 18.0f;
    private float rotateSpeed = 0.25f;

    private float forward;

    public float Rundirection = 0;
    private float RunSpeed = 5.0f;

    // Use this for initialization
    void Awake()
    {
        mainCamera = Camera.main.transform;
        anim = GetComponent<Animator>();
        transform.gameObject.AddComponent<SkillController>();
        
    }
    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        forward = mainCamera.transform.rotation.eulerAngles.y;
        //W键或者上方向键按下的时候
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            MoveByDirection(forward);
        }
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            MoveByDirection(forward - 180);
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            MoveByDirection(forward + 90);

        }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            MoveByDirection(forward - 90);
        }
        
        //W键或者上方向键抬起的时候执行idle动画
        if (!Input.anyKey)
        {
            anim.SetBool("Run", false);
        }

    }
    private void MoveByDirection(float dir)
    {
        anim.SetBool("Run", true);
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
            Quaternion.Euler(this.transform.rotation.x, dir, this.transform.rotation.z), changDirSpeed * Time.deltaTime);
        this.GetComponent<CharacterController>().Move(transform.forward * Time.deltaTime * RunSpeed);
        Rundirection = horizontal;
        anim.SetFloat("RunDir", Rundirection);

    }
}